package com.liam.micro.consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("rateLimit")
public class RateLimitController {


    @GetMapping("bySourceName")
    @SentinelResource(value = "bySourceName",blockHandler = "handlerException")
    public Map<String,Object> bySourceName(){
        System.out.println("bySourceName");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("msg","按资源名称限流");
        map.put("code",200);
        return map;
    }


    /**
     * 触发限流
     * @param ex
     * @return
     */
    public Map<String,Object> handlerException(BlockException ex){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("msg",ex);
        map.put("code",200);
        return map;
    }

}
